/*
 * SelectItemDialog.java
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.admin.gui.dialogs;

import org.fpdev.util.gui.GUIFactory;
import java.awt.event.*;
import java.util.Enumeration;
import java.util.Iterator;

import javax.swing.*;
import javax.swing.border.*;
import org.fpdev.apps.admin.gui.*;

public class SelectItemDialog extends JDialog implements ActionListener {

  private JButton okBtn_,  cancelBtn_;
  private ButtonGroup rBtnGrp_;
  //private String addr_;
  private int selIndex_;

  public SelectItemDialog(JFrame frame, Iterator<String> addrs) {
    super(frame, "Select Dialog", true);
    //addr_ = null;
    selIndex_ = -1;

    okBtn_ = new JButton("OK");
    okBtn_.addActionListener(this);
    cancelBtn_ = new JButton("Cancel");
    cancelBtn_.addActionListener(this);

    JPanel btnRow = new JPanel();
    btnRow.setLayout(new BoxLayout(btnRow, BoxLayout.X_AXIS));
    btnRow.add(okBtn_);
    btnRow.add(Box.createHorizontalStrut(5));
    btnRow.add(cancelBtn_);

    JPanel itemPanel = new JPanel();
    itemPanel.setLayout(new BoxLayout(itemPanel, BoxLayout.Y_AXIS));

    rBtnGrp_ = new ButtonGroup();
    int numItems = 0;
    while (addrs.hasNext()) {
      JRadioButton rBtn = new JRadioButton(addrs.next());
      rBtnGrp_.add(rBtn);
      rBtn.addActionListener(this);
      itemPanel.add(rBtn);
      numItems++;
    }

    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
    topPanel.add(new JLabel("Please specify the specific address:"));
    topPanel.add(Box.createVerticalStrut(4));
    if (numItems <= 8) {
      topPanel.add(itemPanel);
    } else {
      JScrollPane scrollPane = new JScrollPane(itemPanel);
      //scrollPane.setSize(new java.awt.Dimension(250,150));
      GUIFactory.setSize(scrollPane, 300, 180);
      scrollPane.setAlignmentX(JComponent.LEFT_ALIGNMENT);
      topPanel.add(scrollPane);

    }

    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

    topPanel.setAlignmentX(CENTER_ALIGNMENT);
    mainPanel.add(topPanel);
    mainPanel.add(Box.createVerticalStrut(6));
    btnRow.setAlignmentX(CENTER_ALIGNMENT);
    mainPanel.add(btnRow);

    mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(mainPanel);

    this.pack();
    this.setLocation((frame.getWidth() / 2) - getWidth() / 2 + frame.getX(),
            (frame.getHeight() / 2) - getHeight() / 2 + frame.getY());
    show();
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == okBtn_) {
      /*System.out.println("num btns in grp: "+rBtnGrp_.getButtonCount());
      Object[] selBtns = rBtnGrp_.getSelection().getSelectedObjects();
      if(selBtns==null) System.out.println("selBtns is null");
      if(selBtns.length == 1) {
      addr_ = ((JRadioButton) selBtns[0]).getText();
      }
      else addr_ = null;*/
      this.hide();
    } else if (e.getSource() == cancelBtn_) {
      //addr_ = null;
      selIndex_ = -1;
      this.hide();
    } else if (e.getSource() instanceof JRadioButton) {
      Enumeration btns = rBtnGrp_.getElements();
      int i = 0;
      while (btns.hasMoreElements()) {
        if (btns.nextElement() == e.getSource()) {
          break;
        }
        i++;
      }
      selIndex_ = i;
    }
  }

  public int getSelectedIndex() {
    return selIndex_;
  }
}




















